package com.tornado.system.controller;


import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tornado.system.model.News;
import com.tornado.system.service.InewsService;

/*
 * 新闻的控制界面
 */
@Controller
@RequestMapping("/news")
public class newsController {
	
	@Autowired
private InewsService inewsService;
	
	/*
	 * 查询新闻
	 */
	@RequestMapping(value="/list")
	public String newsList(Model model) throws ParseException{
		// 取值存值
		List<News> newsList = inewsService.selectAll();

		// 存值
		model.addAttribute("news", newsList);
		return "/newsDetail";
		
	}

	/*
	 * 根据ID查询新闻
	 */
	@RequestMapping(value = "/listOne")
	public String newsListById(int newsId,Model model) {
		News newsList = inewsService.selectByPrimaryKey(newsId);
		model.addAttribute("news", newsList);
		return "/newsDetail";
		
	}

	/*
	 * 新增新闻
	 */
	@RequestMapping(value="insertNews")
	public String insertNews(News record) throws ParseException{
		
		//手动尝试该方法是否灵验
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse("2017-08-04");
		News news = new News("三生三世", "刘亦菲", date, "一部靠胭脂");//创建一个News
		
		
		int result = inewsService.insert(news);//留待判断新增是否成功
		
		
		System.out.println("aaaaaaaaa"+result);
		return "";
	}
	
	/*
	 * 更新新闻
	 */
	@RequestMapping(value="/updateNews")
	public String updateNews(News record){
		News records = new News(5,"战狼5", "吴京", "战美人鱼"); 
		
	int result = inewsService.updateByPrimaryKeyWithBLOBs(records);
	
	System.out.println("aaaaaaaaa"+result);
		return "";
	}
	
	/*
	 * 删除新闻
	 */
	@RequestMapping(value="/deleteNews")
	public String deleteNews(Integer newsId){
		
	int result =	inewsService.deleteByPrimaryKey(6);
	
	System.out.println("aaaaaaaaa"+result);
		return "";
	}
	
}
